package Thread;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;


public class Main {

    public static void main4(String[] args) {
        // 拒绝策略为CallerRunsPolicy
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                5,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        // 提交10个任务，尝试超出线程池的处理能力
        for (int i = 0; i < 10; i++) {
            int taskNumber = i;
            try {
                // 提交任务到线程池
                executor.execute(() -> {
                    System.out.println("执行的任务 " + taskNumber + " 在线程 " + Thread.currentThread().getName());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    System.out.println("任务 " + taskNumber + " 完成");
                });
            } catch (RejectedExecutionException e) {
                // 如果CallerRunsPolicy被触发，这里不会捕获到RejectedExecutionException
                // 因为CallerRunsPolicy不会抛出异常，而是让调用者线程运行任务
                System.out.println("任务 " + taskNumber + " 被拒绝，并将由调用者线程 " + Thread.currentThread().getName() + " 运行");
            }
        }

        // 关闭线程池
        executor.shutdown();
        try {
            // 等待线程池中的所有任务完成
            if (!executor.awaitTermination(60, TimeUnit.MINUTES)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
        }
    }

    /*
    public static void main4(String[] args) {
        // 创建一个具有可变大小的线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2, // 核心线程数
                10, // 最大线程数
                60L, // 空闲线程保持活动时间
                TimeUnit.SECONDS, // 时间单位
                new LinkedBlockingQueue<Runnable>(), // 工作队列
                new CustomThreadFactory() // 自定义线程工厂，用于跟踪线程创建
        );

        // 提交任务到线程池
        for (int i = 0; i < 20; i++) {
            int taskNumber = i;
            executor.execute(() -> {
                System.out.println("Task " + taskNumber + " is being processed by " + Thread.currentThread().getName());
                try {
                    // 模拟任务执行时间
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }

        // 关闭线程池
        executor.shutdown();
        try {
            // 等待线程池中的所有任务完成
            if (!executor.awaitTermination(2, TimeUnit.MINUTES)) {
                executor.shutdownNow(); // 超时后强制关闭
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
        }

        // 打印线程池的详细信息
        System.out.println("Largest pool size: " + executor.getLargestPoolSize());
        System.out.println("Pool size: " + executor.getPoolSize());
        System.out.println("Active count: " + executor.getActiveCount());
        System.out.println("Completed task count: " + executor.getCompletedTaskCount());
    }

     */

    // 自定义线程工厂，用于跟踪线程创建
    /*
    private static class CustomThreadFactory implements ThreadFactory {
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);

        public CustomThreadFactory() {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r, "pool-thread-" + threadNumber.getAndIncrement(), 0);
            System.out.println("Thread created: " + t.getName());
            return t;
        }
    }
    */
    public static void main3(String[] args) {
        try {
            BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(Integer.MAX_VALUE + 100);
            System.out.println("队列创建成功，容量为: " + Long.MAX_VALUE);
        } catch (OutOfMemoryError e) {
            System.err.println("创建队列失败: " + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.err.println("创建队列失败: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("创建队列失败: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        // 创建一个具有可变大小的线程池
        // 核心线程数为2，最大线程数为10，空闲线程保持活动时间为1分钟
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                3,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(4) // 工作队列
        );

        // 提交一些任务
        for (int i = 0; i < 10; i++) {
            final int taskId = i;
            executor.execute(() -> {
                System.out.println("任务 " + taskId + " 运行在 " + Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务 " + taskId + " 完成.");
            });
        }

        // 关闭线程池
        executor.shutdown();
        try {
            if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        String s2 = in.nextLine();

        for(int i = 0;i < s2.length();i++) {
            char c = s2.charAt(i);
            if(s1.indexOf(i) == c) {
                s1 = s1.substring(0,i)+s1.substring(i+1,s2.length());
            }
        }
        System.out.println(s1);
    }

//    public static String reveseStr(String str) {
//        String[] s = str.split(" ");
//        StringBuilder stringBuilder = new StringBuilder();
//        for(int i = s.length - 1;i>=0;i--) {
//            stringBuilder.append(s[i]);
//            if(i > 0) {
//                stringBuilder.append(" ");
//            }
//        }
//        return stringBuilder.toString();
//    }
//
//
//    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
//        String str = in.nextLine();
//        String res = reveseStr(str);
//        System.out.println(res);
//    }
}